package jp.co.hcs.attendancemanagement.util;

import java.util.List;

/**
 * The Class TimeUtil.
 */
public class TimeUtil {

  /** The Constant TIME_EMPTY. */
  public static final String TIME_EMPTY = "00:00";

  /**
   * Checks if is equal.
   * 
   * @param time1 the time1
   * @param time2 the time2
   * @return true, if is equal
   */
  public static boolean isEqual(String time1, String time2) {
    return time1.equals(time2);
  }

  /**
   * Checks if is less than.
   * 
   * @param time1 the time1
   * @param time2 the time2
   * @return true, if is less than
   */
  public static boolean isLessThan(String time1, String time2) {
    return isGreaterThan(time2, time1);
  }

  /**
   * Checks if is greater than.
   * 
   * @param time1 the time1
   * @param time2 the time2
   * @return true, if is greater than
   */
  public static boolean isGreaterThan(String time1, String time2) {
    if (StringUtil.isEmpty(time1)) {
      time1 = TIME_EMPTY;
    }
    if (StringUtil.isEmpty(time2)) {
      time2 = TIME_EMPTY;
    }

    String[] timeSpliter;

    timeSpliter = time1.split(":");
    int hour1 = Integer.parseInt(timeSpliter[0]);
    int minute1 = Integer.parseInt(timeSpliter[1]);

    timeSpliter = time2.split(":");
    int hour2 = Integer.parseInt(timeSpliter[0]);
    int minute2 = Integer.parseInt(timeSpliter[1]);

    if (hour1 > hour2) {
      return true;
    } else if (hour1 < hour2) {
      return false;
    } else {
      if (minute1 > minute2) {
        return true;
      } else {
        return false;
      }
    }
  }

  /**
   * Checks if is less than or equal.
   * 
   * @param time1 the time1
   * @param time2 the time2
   * @return true, if is less than or equal
   */
  public static boolean isLessThanOrEqual(String time1, String time2) {
    return isGreaterThanOrEqual(time2, time1);
  }

  /**
   * Checks if is greater than or equal.
   * 
   * @param time1 the time1
   * @param time2 the time2
   * @return true, if is greater than or equal
   */
  public static boolean isGreaterThanOrEqual(String time1, String time2) {
    return isGreaterThan(time1, time2) || isEqual(time1, time2);
  }

  /**
   * Convert time from hhmm.
   * 
   * @param time the time
   * @return the float
   */
  public static float convertTimeFromHHMM(String time) {
    String[] timeSpliter = time.split(":");
    int hour = Integer.parseInt(timeSpliter[0]);
    int minute = Integer.parseInt(timeSpliter[1]);

    return hour + (float) minute / 60;
  }

  /**
   * Convert time from float.
   * 
   * @param floatTime the float time
   * @return the string
   */
  public static String convertTimeFromFloat(float floatTime) {
    int hour = (int) floatTime;
    float floatMinute = floatTime - hour;
    return StringUtil.parseZeroString(hour) + ":"
        + StringUtil.parseZeroString((int) (floatMinute * 60));
  }

  /**
   * Calculate total time.
   * 
   * @param items the items
   * @return the string
   */
  public static String calculateTotalTime(List<String> items) {
    String[] timeSpliter;
    int totalHour = 0;
    int totalMinute = 0;
    for (String time : items) {
      if (!StringUtil.isEmpty(time)) {
        timeSpliter = time.split(":");
        totalHour += Integer.parseInt(timeSpliter[0]);
        totalMinute += Integer.parseInt(timeSpliter[1]);
      }
    }

    totalHour += totalMinute / 60;
    totalMinute = totalMinute % 60;

    return StringUtil.parseZeroString(totalHour) + ":" + StringUtil.parseZeroString(totalMinute);
  }

  /**
   * Sum.
   * 
   * @param time1 the time1
   * @param time2 the time2
   * @return the string
   */
  public static String sum(String time1, String time2) {
    if (StringUtil.isEmpty(time1)) {
      time1 = TIME_EMPTY;
    }
    if (StringUtil.isEmpty(time2)) {
      time2 = TIME_EMPTY;
    }

    String[] timeSpliter;

    timeSpliter = time1.split(":");
    int hour1 = Integer.parseInt(timeSpliter[0]);
    int minute1 = Integer.parseInt(timeSpliter[1]);

    timeSpliter = time2.split(":");
    int hour2 = Integer.parseInt(timeSpliter[0]);
    int minute2 = Integer.parseInt(timeSpliter[1]);

    int totalHour = hour1 + hour2;
    int totalMinute = minute1 + minute2;

    totalHour += totalMinute / 60;
    totalMinute = totalMinute % 60;

    if (totalHour > 24) {
      totalHour = 24;
    }

    return StringUtil.parseZeroString(totalHour) + ":" + StringUtil.parseZeroString(totalMinute);
  }

  /**
   * Sub.
   * 
   * @param time1 the time1
   * @param time2 the time2
   * @return the string
   */
  public static String sub(String time1, String time2) {
    if (StringUtil.isEmpty(time1)) {
      time1 = TIME_EMPTY;
    }
    if (StringUtil.isEmpty(time2)) {
      time2 = TIME_EMPTY;
    }

    if (isLessThan(time1, time2)) {
      return sub(time2, time1);
    }

    String[] timeSpliter;

    timeSpliter = time1.split(":");
    int hour1 = Integer.parseInt(timeSpliter[0]);
    int minute1 = Integer.parseInt(timeSpliter[1]);

    timeSpliter = time2.split(":");
    int hour2 = Integer.parseInt(timeSpliter[0]);
    int minute2 = Integer.parseInt(timeSpliter[1]);

    int totalHour = hour1 - hour2;
    if (minute1 < minute2) {
      totalHour--;
      minute1 += 60;
    }
    int totalMinute = minute1 - minute2;

    if (totalHour < 0) {
      totalHour = 0;
    }

    return StringUtil.parseZeroString(totalHour) + ":" + StringUtil.parseZeroString(totalMinute);
  }

  /**
   * Checks if is time empty.
   * 
   * @param time the time
   * @return true, if is time empty
   */
  public static boolean isTimeEmpty(String time) {
    return StringUtil.isEmpty(time) || TIME_EMPTY.equals(time);
  }

  /**
   * Round time.
   * 
   * @param time the time
   * @param roundingTime the rounding time
   * @return the string
   */
  public static String roundTime(String time, int roundingTime) {
    if (isTimeEmpty(time)) {
      return TIME_EMPTY;
    }
    String[] timeSpliter = time.split(":");
    int tmpHour = Integer.parseInt(timeSpliter[0]);
    int tmpMinute = Integer.parseInt(timeSpliter[1]);
    tmpMinute =
        (tmpMinute / roundingTime) * roundingTime
            + (tmpMinute % roundingTime > 0 ? roundingTime : 0);
    if (tmpMinute == 60) {
      tmpHour = tmpHour + 1;
      tmpMinute = 0;
    }
    return StringUtil.parseZeroString(tmpHour) + ":" + StringUtil.parseZeroString(tmpMinute);
  }

}
